package com.qiche.util;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DateTimeUtils : 日期时间处理工具类
 *
 * @author zhangds
 * @since 2017-04-17 15:28
 */
public class DateTimeUtils {

	/**
	 * 把字符串转日期
	 *
	 * @param date
	 * @param formats
	 * @return
	 * @throws ParseException
	 */
	public static Date str2Date(String date, String formats) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(formats);
		return sdf.parse(date);
	}

	/**
	 * 读取指定格式的当前系统时间字符串
	 *
	 * @param formats
	 *            指定日期格式
	 * @return
	 */
	public static String getFormatDate(String formats) {
		Date date = new Date();
		DateFormat sdf = new SimpleDateFormat(formats);
		String dateStr = sdf.format(date);

		return dateStr;
	}

	/**
	 * 获取一个指定时间的指定格式字符串
	 *
	 * @param date
	 *            指定日期
	 * @param formats
	 *            指定日期格式
	 * @return
	 */
	public static String getFormatDate(Date date, String formats) {
		DateFormat sdf = new SimpleDateFormat(formats);
		String dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * 读取指定日期的Unix时间戳
	 *
	 * @param date
	 *            指定日期字符串 格式为yyyy-MM-dd HH:mm:ss
	 * @return Unix时间戳
	 */
	public static Long getTimestamp(String date) {
		Long timestamp = 0L;
		try {
			Calendar cal = Calendar.getInstance();
			DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date first_time = fmt.parse("1970-1-1 8:0:0");
			Date second_time = fmt.parse(date);
			cal.setTime(second_time);
			Long time1 = cal.getTimeInMillis();
			cal.setTime(first_time);
			Long time2 = cal.getTimeInMillis();
			timestamp = (time1 - time2) / 1000;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return timestamp;
	}

	/**
	 * 读取当前系统时间的Unix时间戳
	 *
	 * @return Unix时间戳
	 */
	public static Long getTimestamp() {
		Long timestamp = 0L;
		try {
			Calendar cal = Calendar.getInstance();
			DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date first_time = fmt.parse("1970-1-1 8:0:0");
			Date second_time = new Date();
			cal.setTime(second_time);
			Long time1 = cal.getTimeInMillis();
			cal.setTime(first_time);
			Long time2 = cal.getTimeInMillis();
			timestamp = (time1 - time2) / 1000;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return timestamp;
	}

	/**
	 * 判断时间是今天还是昨天
	 *
	 * @param time
	 * @return
	 */
	public static boolean formatDateTime(String time) {
		SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		int year1 = c1.get(Calendar.YEAR);
		int month1 = c1.get(Calendar.MONTH) + 1;
		int day1 = c1.get(Calendar.DAY_OF_MONTH);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(new Date());
		int year2 = c2.get(Calendar.YEAR);
		int month2 = c2.get(Calendar.MONTH) + 1;
		int day2 = c2.get(Calendar.DAY_OF_MONTH);
		if (year1 == year2 && month1 == month2 && day1 == day2) {
			return true;// 今天
		}
		return false;// 不是今天
	}

	/**
	 * 计算两个日期之间相差的天数
	 *
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 获取当前日期是星期几<br>
	 *
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 读取指定日期的年份
	 *
	 * @param date
	 * @return
	 */
	public static Integer getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 读取指定日期的月份
	 *
	 * @param date
	 * @return
	 */
	public static Integer getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 读取指定日期天数
	 *
	 * @param date
	 * @return
	 */
	public static Integer getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DATE);
	}

	/**
	 * 获取今天之前或者之后的日期
	 *
	 * @param i
	 *            今天之前或者之后的天数
	 * @return
	 */
	public static Date getDate(Integer i) {
		Date returndate = null;
		try {
			Date date = new Date();// 取时间
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			calendar.add(calendar.DATE, i == null ? 0 : i);// 把日期往后增加一天.整数往后推,负数往前移动
			date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(date);
			returndate = str2Date(dateString, "yyyy-MM-dd HH:mm:ss");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return returndate;

	}

	/**
	 * 获取指定日期之前或者之后的日期
	 *
	 * @param date
	 *            指定日期
	 * @param i
	 *            今天之前或者之后的天数
	 * @return
	 */
	public static Date getDate(Date date, Integer i) {
		Date returndate = null;
		try {
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			calendar.add(calendar.DATE, i == null ? 0 : i);// 把日期往后增加一天.整数往后推,负数往前移动
			date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(date);
			returndate = str2Date(dateString, "yyyy-MM-dd HH:mm:ss");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return returndate;

	}

	/**
	 * 判断指定字符串是否日期格式
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isValidDate(String str) {
		// 使用正则表达式 测试 字符 符合 dddd-dd-dd 的格式(d表示数字)
		Pattern p = Pattern.compile("\\d{4}+[-]\\d{1,2}+[-]\\d{1,2}+");
		Matcher m = p.matcher(str);
		if (!m.matches()) {
			return false;
		}

		// 得到年月日
		String[] array = str.split("-");
		int year = Integer.valueOf(array[0]);
		int month = Integer.valueOf(array[1]);
		int day = Integer.valueOf(array[2]);

		if (month < 1 || month > 12) {
			return false;
		}
		int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (isLeapYear(year)) {
			monthLengths[2] = 29;
		} else {
			monthLengths[2] = 28;
		}
		int monthLength = monthLengths[month];
		if (day < 1 || day > monthLength) {
			return false;
		}
		return true;
	}


	public static int isExpriedByPolicy(Date date ,Integer day){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR,1);
		Date newDate=calendar.getTime();
		if(new Date().getTime()>newDate.getTime()){
			//过期
			return -1;
		}else if((newDate.getTime()-new Date().getTime())/1000/60/60/24 < day) {
			//即将过期
			return 0;
		}else {
			//正常的
			return 1;
		}

	}
	/** 是否是闰年 */
	private static boolean isLeapYear(int year) {
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	}

	public static void main(String[] args) throws ParseException {

	int i=	isExpriedByPolicy(new Date("2017/5/8"),30);

		System.out.println(i);

	}

	// 判断选择的日期是否是本周
	public static boolean isThisWeek(long time) {
		Calendar calendar = Calendar.getInstance();
		int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
		calendar.setTime(new Date(time));
		int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
		if (paramWeek == currentWeek) {
			return true;
		}
		return false;
	}

	// 判断选择的日期是否是今天
	public static boolean isToday(long time) {
		return isThisTime(time, "yyyy-MM-dd");
	}

	// 判断选择的日期是否是本月
	public static boolean isThisMonth(long time) {
		return isThisTime(time, "yyyy-MM");
	}

	private static boolean isThisTime(long time, String pattern) {
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String param = sdf.format(date);// 参数时间
		String now = sdf.format(new Date());// 当前时间
		if (param.equals(now)) {
			return true;
		}
		return false;
	}
	
	 /**
     * 给时间加上半小时
     * @param day 当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public static String addDateMinut(String day, int hour){   
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;   
        try {   
            date = format.parse(day);   
        } catch (Exception ex) {   
            ex.printStackTrace();   
        }   
        if (date == null)   
            return "";   
        Calendar cal = Calendar.getInstance();   
        cal.setTime(date);   
        cal.add(Calendar.MINUTE, hour);// 24小时制   
        date = cal.getTime();   
        cal = null;   
        return format.format(date);   

    }

}
